package lib::Noplagia;

# This file is part of noplagia.
#
# noplagia is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# noplagia is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# noplagia; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307 USA

use strict;
use WWW::Mechanize;
use Data::Dumper; # FIXME :: remove...
use lib::TextTreatment;
use lib::Context;
use File::Spec;
use Encode;

require Exporter;
our ($VERSION,@ISA,@EXPORT,$context);

$VERSION = 0.01;

=head1 NAME

    TODO

=head1 SYNOPSIS

    TODO

=head1 DESCRIPTION

    TODO

=head1 FUNCTIONS

=over 2

=cut

@ISA    = qw(Exporter);
@EXPORT = qw(
    &noplagia
);

=item noplagia

    my $results = noplagia(
        {
            type  => text
            value => $text
            dest  => $dest
        }
    );

=cut

sub noplagia {
    my $in = shift;
    my $text;
    lib::Context::set_conn_db();
    my $schema = get_dbh();
    
    # -------------------------------
    # -1- Input
    # -------------------------------
    if($in->{type} eq "text"){
        require lib::Input::Text;
        $text = lib::Input::Text::get_text($in->{value});

        #insertion dans la bdd
        my $file = $schema->resultset('Users')->find({pseudo => $ENV{"NOPLAGIA_SESSION"}->param('login')})->add_to_files({content => $text});

        #insertion de la tache dans la file d'attente
        my $task = $schema->resultset('TaskQueue')->new({files => $file->id});
        $task->insert;
        
        foreach(@{$in->{dest}}){
            warn "report to user id =>".$_;
            my $user_id = $schema->resultset('Users')->find(
                {pseudo=>$_}
            )->id;
            $file->add_to_reports(
                {
                    files => $file->id,
                    users => $user_id,
                }
            );
        }
    }
    if($in->{type} eq "pdf"){
        #recuperation du fichier uploader
        use CGI::Upload;
        my $upload = CGI::Upload->new;
        my $file_handle = $upload->file_handle('value');

        #insertion dans le depot de fichier
        use lib::Storage;
        my $path = &handle_insert($file_handle, $in->{value}, $ENV{"NOPLAGIA_SESSION"}->param('login'));

        use lib::Input::Pdf;
        $text = lib::Input::Pdf::get_text(File::Spec->catfile(config("filepool"), $path));

        #insertion dans la base de donnée (update car deja inserer par le module Storage.pm
        my $file = $schema->resultset('Files')->find({path => $path});
        $file->update({content => $text});

        #insertion de la tache dans la file d'attente
        $file->add_to_taskqueue({});

        foreach(@{$in->{dest}}){
            my $user_id = $schema->resultset('Users')->find(
                {pseudo=>$_}
            )->id;
            warn "report to user id =>".$user_id;
            $file->add_to_reports(
                {
                    files => $file->id,
                    users => $user_id,
                }
            );
        }

    }
    if($in->{type} eq "uri"){
        require lib::Input::Html;
        $text = lib::Input::Html::get_text($in->{value});
        #insertion dans la bdd
        my $file = $schema->resultset('Users')->find({pseudo => $ENV{"NOPLAGIA_SESSION"}->param('login')})->add_to_files({content => $text});

        #insertion de la tache dans la file d'attente
        my $task = $schema->resultset('TaskQueue')->new({files => $file->id});
        $task->insert;

       foreach(@{$in->{dest}}){
            my $user_id = $schema->resultset('Users')->find(
                {pseudo=>$_}
            )->id;
            warn "report to user id =>".$user_id;
            $file->add_to_reports(
                {
                    files => $file->id,
                    users => $user_id,
                }
            );
        }
    }
}

END { }
1;
__END__

=back

=head1 AUTEUR

    noplagia dev team

    http://noplagia.org

=cut
